from selenium.webdriver.common.by import By

from base.page_base import PageBase


class LoginPage(PageBase):
    """登录页面"""

    # 元素定位
    # 1.同意
    __agree = (By.XPATH, '//*[@text="同意"]')
    # 2.个人
    __mine = (By.XPATH, '//*[@text="个人"]')
    # 3.切换登录方式
    __login_type = (By.XPATH, '(//android.widget.ImageView[@resource-id="com.netease.yanxuan:id/img_icon"])[2]')
    # 4.其他登录方式
    __other_login = (By.ID, 'com.netease.yanxuan:id/newOtherLoginTypes')
    # 5.勾选协议
    __agree_checkbox = (By.XPATH, '//android.widget.CheckBox[@resource-id="com.netease.yanxuan:id/check_box"]')
    # 6.点击邮箱登录
    __email_login = (By.XPATH, '(//android.widget.ImageView[@resource-id="com.netease.yanxuan:id/img_icon"])[4]')
    # 7.输入账号密码,点击登录
    __username = (By.ID, 'com.netease.yanxuan:id/account_edit')
    __password = (By.ID, 'com.netease.yanxuan:id/password_edit')
    __login_btn = (By.ID, 'com.netease.yanxuan:id/btn_login_content')
    # 8.获取成功文本
    __success_text = (By.XPATH, '//*[@text="用户13015504"]')

    # 9.获取登录失败文本
    __fail_text = (By.XPATH, '//android.widget.Toast')

    # 操作方法
    def click_agree(self):
        """点击同意"""
        self.base_click(self.__agree)

    def click_mine(self):
        """点击个人"""
        self.base_click(self.__mine)

    def click_switch_login_type(self):
        """点击切换登录方式"""
        self.base_click(self.__login_type)
        self.base_click(self.__other_login)
        self.base_click(self.__agree_checkbox)
        self.base_click(self.__email_login)

    def input_username(self, username):
        """输入邮箱账号"""
        self.base_input_text(self.__username, username)

    def input_password(self, password):
        """输入密码"""
        self.base_input_text(self.__password, password)

    def click_login_btn(self):
        """点击登录按钮"""
        self.base_click(self.__login_btn)

    # 业务流程
    def login(self, username, password):
        """业务:登录操作"""
        self.click_switch_login_type()
        self.input_username(username)
        self.input_password(password)
        self.click_login_btn()

    def get_success_text(self):
        """获取成功文本"""
        return self.base_get_text(self.__success_text)

    def get_fail_text(self):
        """获取失败文本"""
        return self.base_get_toast(self.__fail_text)
